#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(){
    int fd[2];
    pid_t pid;
    char *str = "hello pipe\n";
    char buf[1024];
    int ret = pipe(fd);
    if (ret == -1){
        perror("pipe error");
        exit(1);
    }
    pid = fork();
    if(pid > 0){
        close(fd[0]);//关闭读
        write(fd[1],str,strlen(str));
        close(fd[1]);
    }else if(pid == 0){
        close(fd[1]);
        ret = read(fd[0],buf,sizeof(buf));
        write(STDOUT_FILENO,buf,ret);
        close(fd[0]);
    }
    return 0;

}